package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;
import com.zjcode.cdcer.model.CerTreatmentInfo;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerTreatmentInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 人群筛查系统
 * 治疗：cer_treatment_info
 */
@RestController
@Slf4j
@RequestMapping("/v1/treatment/")
public class CerTreatmentInfoController  extends BaseController {

    @Autowired
    CerTreatmentInfoService cerTreatmentInfoService;

    /**
     * 新增治疗
     * 初始上报满足条件的数据加入到治疗列表中
     * @param cerTreatmentInfo
     * @return
     */
    @PostMapping(path = "/add")
    public JsonObj add(@Validated(InsertValidator.class) @RequestBody CerTreatmentInfo cerTreatmentInfo) {
        cerTreatmentInfoService.insertTreatment(cerTreatmentInfo);
        return JsonObj.success("新增成功");
    }

    /**
     * 编辑治疗
     * @param cerTreatmentInfo
     * @return
     */
    @PostMapping(path = "/update")
    public JsonObj update(@Validated(UpdateValidator.class) @RequestBody CerTreatmentInfo cerTreatmentInfo) {
        cerTreatmentInfoService.updateTreatment(cerTreatmentInfo);
        return JsonObj.success("修改成功");
    }

    /**
     * 查治疗详情
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public JsonObj findById(@RequestParam String id) {
        return cerTreatmentInfoService.findById(id);
    }

    /**
     * 查询治疗列表
     */
    @PostMapping("/listAllData")
    public ListResult<Object> listAllData(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(cerTreatmentInfoService.findAllList(pageData));
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public JsonObj deleteById(@RequestParam String id) {
        return cerTreatmentInfoService.deleteById(id);
    }

    @PostMapping("queryAllExportData")
    @ApiOperation("点击导出按钮，获取所有的治疗信息列表")
    public JsonObj queryAllExportData(@RequestBody PageData pageData) {
        List<LinkedHashMap<String,Object>> list  = cerTreatmentInfoService.queryAllExportData(pageData);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return JsonObj.success(map);
    }

    @PostMapping(value = "/importGJData")
    @ApiOperation("导入（国家格式）")
    public JsonObj importGJData(@RequestParam("file") MultipartFile file, HttpServletResponse response)
            throws IOException {
        PageData pageData = getPageData();
        String userId = (String) pageData.get("user");
        return cerTreatmentInfoService.importGJData(file,response,userId);
    }

}
